<template>
	<view class="box">
		<img src="../../static/img/codeBg.png" class="bg-img" />
		<view class="title">券码仅限使用一次</view>
		<img src="../../static/img/left.png" class="left-btn btn" @click="leftBtn" />
		<img src="../../static/img/right.png" class="right-btn btn" @click="rightBtn" />
		<view class="code-bg" v-if="codeWidth && list.length">
			<view class="code-bg-detai" v-for="(item, index2) in list" :key="index2">
				<codeT v-if="index == index2" :size="codeWidth" :value="item.code" />
			</view>
		</view>
		<view class="detail-text">{{ listText }}</view>
		<view class="other">
			<view>温馨提示:</view>
			<view>1.二维码有效期至2025年5月31日，请尽快使用</view>
			<view>2.到店柜台扫码核销，跟店员说明，用“自有券码”核销</view>
			<view>3.打包需收取1元打包费</view>
		</view>
	</view>
</template>

<script>
	import codeT from "@/uni_modules/lime-qrcode/components/l-qrcode/l-qrcode.vue";

	export default {
		components: {
			codeT,
		},
		data() {
			return {
				codeWidth: 0,
				list: [],
				index: 0,
				listText: "",
			};
		},
		onLoad() {
			const p = wx.getStorageSync('NODE_ENV')
			this.api = p == 'prod' ? 'https://qy.mtasia.net/wph5api_zgfqy': 'https://qy.mtasia.net/wph5api_dev'
			// 动态渲染二维码大小
			const systemInfo = wx.getSystemInfoSync();
			const screenWidth = systemInfo.screenWidth;
			this.codeWidth = (screenWidth / 750) * 320;
			this.list = [
			];
			this.getCodeList()
		},
		methods: {
			getCodeList() {
					// this.list =  [{
					// 		"code": "GDCX_GDZSY-CXLBQY20YMJQ"
					// 	},
					// 	{
					// 		"code": "GDCX_GDZSY-CXTCS15YMJQ"
					// 	}
					// ]
					// return;
				uni.showLoading({
					mask: true,
				})
				uni.request({
					url: this.api + '/dg/zgfqy/h5/queryByPhone',
					method: 'GET',
					header: {
						Authorization: wx.getStorageSync('token'),
					},
					data: {
					},
					success: res => {
						console.log(res.data)
						// res = {
						// 	"code": "200",
						// 	"message": "成功",
						// 	"data": [{
						// 			"code": "GDCX_GDZSY-CXLBQY20YMJQ"
						// 		},
						// 		{
						// 			"code": "GDCX_GDZSY-CXTCS15YMJQ"
						// 		}
						// 	]
						// }
						uni.hideLoading()
						if (res.data.code == 200) {
							this.list = res.data.data
							this.setCode();
						} else {
							uni.showToast({
								title: res.data.message,
								icon: 'error'
							})
						}
					}
				})
			},
			leftBtn() {
				if (this.index == 0) {
					return;
				}
				this.index--;
				this.setCode();
			},
			rightBtn() {
				if (this.index + 1 == this.list.length) {
					return;
				}
				this.index++;
				this.setCode();
			},
			setCode() {
				console.log(this.index);
				this.listText = this.list[this.index].name;
			},
		},
	};
</script>

<style>
	.box {
		width: 750rpx;
		min-height: 100vh;
		overflow-y: auto;
		background: #fff8e5;
	}

	.bg-img {
		width: 100%;
		height: 1334rpx;
	}

	.title {
		font-weight: bold;
		font-size: 43rpx;
		color: #0c0c0c;
		position: absolute;
		top: 419rpx;
		width: 100%;
		text-align: center;
	}

	.btn {
		width: 68rpx;
		height: 68rpx;
		position: absolute;
		top: 634rpx;
	}

	.left-btn {
		left: 70rpx;
	}

	.right-btn {
		right: 70rpx;
	}

	.code-bg {
		width: 359rpx;
		height: 359rpx;
		background: #ffffff;
		border-radius: 35rpx;
		border: 4px solid #000000;
		position: absolute;
		top: 499rpx;
		left: 192rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.detail-text {
		width: 84%;
		text-align: center;
		font-size: 42rpx;
		color: #0c0c0c;
		top: 890rpx;
		left: 8%;
		position: absolute;
		font-weight: bold;
	}

	.other {
		width: 520rpx;
		top: 1003rpx;
		left: 130rpx;
		text-align: left;
		position: absolute;
		color: #000000;
		font-size: 22rpx;
		font-weight: bold;
	}
</style>